<?php
load_info_var($cf_controller);	   //加载smarty变量
load_file('expressfee_class', LIB_PATH);
$main = new ExpressFee($db);
$id = isset($_POST['id']) ? intval($_POST['id']) : intval($_GET['id']);
$self = isset($_POST['self']) ? trim($_POST['self']) : trim($_GET['self']);
$return_page = $cf_pre_page ? $cf_pre_page : $cf_self_page;

switch ($cf_action) {
    case "index":
	$cate = add_array_item(array('' => '全部'), $cf_info_var_common['goods_cate']);
	$smarty->mypage($cf_controller, $cf_action, 0);	
	$skey = isset($_POST['skey']) ? trim($_POST['skey']) : urldecode($_GET['skey']);
	$skey2 = isset($_POST['skey2']) ? trim($_POST['skey2']) : urldecode($_GET['skey2']);
	$skey3 = isset($_POST['skey3']) ? trim($_POST['skey3']) : urldecode($_GET['skey3']);
	$skey4 = isset($_POST['skey4']) ? trim($_POST['skey4']) : urldecode($_GET['skey4']);
	$skey5 = isset($_POST['skey5']) ? trim($_POST['skey5']) : urldecode($_GET['skey5']);	
	$cate1 = isset($_POST['cate1']) ? trim($_POST['cate1']) : trim($_GET['cate1']);
	$cate2 = isset($_POST['cate2']) ? trim($_POST['cate2']) : trim($_GET['cate2']);
	$hot = isset($_POST['hot']) ? trim($_POST['hot']) : trim($_GET['hot']);
	$special = isset($_POST['special']) ? trim($_POST['special']) : trim($_GET['special']);

	
	$where = '1';
	if ($skey) {
	    $where .= " and fee_title like '%" . $skey . "%'";
	}	
	if ($skey2 && $skey3) {
            $where .= " AND DATE_FORMAT(fee_create_time,'%Y-%m-%d')  BETWEEN '".$skey2."' AND '".$skey3."'";
    }
	
	load_file('page_class', LIB_PATH);
	$MyPage = new Mypage($db);
	$field = '*';
	$data_ary = $MyPage->from_sql($field, $main->get_table_name(), $where, "fee_id DESC", intval($_GET['cpage']), "20");
	$page = $MyPage->get_page($data, $cf_self_page . '?dpt=' . $cf_dpt . '&skey=' . $skey . '&skey2=' . $skey2 . '&skey3=' . $skey3 . '&skey4=' . $skey4.'&skey5='.$skey5. '&self=' . $self.'&hot='.$hot.'&special='.$self.'&special='.$self.'&cate1='.$cate1.'&cate2='.$cate2);
	if($data_ary){
		foreach($data_ary as $k=>$v){
			$data_ary[$k]['fee_item'] = unserialize($v['fee_item']);
			
		}	
	}
	
	break;
    case "add":	
	if ($cf_ispost) {
		
		$fee_is_free = $_POST['fee_is_free'];
		if($fee_is_free){//包邮的模板
			$data=array(
						'fee_title'=>trim($_POST['fee_title']),
						'fee_is_free'=>'1',
						'fee_create_time'=>date("Y-m-d H:i:s"),
						'fee_creator'=>$cf_admin_info['admin_name'],
					);
			
		}else{//不包邮模板
			$fee_type = intval($_POST['fee_type']);
			$item_ary = array();
			if($fee_type == '0'){//是按件数计算邮费
				$item_ary['default'] = array(
										'express_start'=>intval($_POST['number_express_start']),
										'express_postage'=>intval($_POST['number_express_postage']),
										'express_plus'=>intval($_POST['number_express_plus']),
										'express_postageplus'=>intval($_POST['number_express_postageplus'])
									);
			}else{//是按重量计算邮费
				$item_ary['default'] = array(
										'express_start'=>intval($_POST['weight_express_start']),
										'express_postage'=>intval($_POST['weight_express_postage']),
										'express_plus'=>intval($_POST['weight_express_plus']),
										'express_postageplus'=>intval($_POST['weight_express_postageplus'])
									);	
			}
			$express_areas = $_POST['express_areas'];
			if(is_array($express_areas)){
				$express_start = $_POST['express_start'];
				$express_postage = $_POST['express_postage'];
				$express_plus = $_POST['express_plus'];
				$express_postageplus = $_POST['express_postageplus'];
				//$item_ary['list'] = '';
				require(INFO_PATH."cf_var_area.php");
				foreach($express_areas as $k=>$v){
					//获取相应的省份名字
					$area_ary = explode(',',$v);
					$areas_name = '';
					foreach($area_ary as $v2){
						$areas_name .= $cf_var_area[$v2]['title'].',';
					}
					$areas_name = trim($areas_name,',');
					
					$temp_ary = array(
									'express_areas_name'=>$areas_name,
									'express_areas'=>$v,
									'express_start'=>$express_start[$k],
									'express_postage'=>$express_postage[$k],
									'express_plus'=>$express_plus[$k],
									'express_postageplus'=>$express_postageplus[$k],
								);
					$item_ary['list'][$k] = $temp_ary;
				}				
			}
			
			
			$data=array(
						'fee_title'=>trim($_POST['fee_title']),
						'fee_is_free'=>'0',
						'fee_type'=>$fee_type,
						'fee_item'=>serialize($item_ary),
						'fee_create_time'=>date("Y-m-d H:i:s"),
						'fee_creator'=>$cf_admin_info['admin_name'],
					);
	
			
		}
		
		$result = $main->insert_data($data);
		if ($result) {
			$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
			unset($_SESSION['pre_url']);
			$more = $_SERVER['HTTP_REFERER'] ? '<a onclick="my_close()" href="' . $_SERVER['HTTP_REFERER'] . '">[继续添加]</a>' : '';
			my_jump('添加成功' . $more, '', $return_page, 3);
	    } else {
			my_jump('添加失败');
	    }			
		
	
	} else {

	    $smarty->mypage($cf_controller, $cf_action, 0);
	    $_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';
		$row['goods_image'] = array();

	    //dump($_REQUEST);
	}

	break;
    case "edit":
	if($cf_ispost) {
	  $fee_is_free = $_POST['fee_is_free'];
		if($fee_is_free){//包邮的模板
			$data=array(
						'fee_title'=>trim($_POST['fee_title']),
						'fee_is_free'=>'1',
						'fee_edit_time'=>date("Y-m-d H:i:s"),
						'fee_editor'=>$cf_admin_info['admin_name'],
					);
			
		}else{//不包邮模板
			$fee_type = intval($_POST['fee_type']);
			$item_ary = array();
			if($fee_type == '0'){//是按件数计算邮费
				$item_ary['default'] = array(
										'express_start'=>intval($_POST['number_express_start']),
										'express_postage'=>intval($_POST['number_express_postage']),
										'express_plus'=>intval($_POST['number_express_plus']),
										'express_postageplus'=>intval($_POST['number_express_postageplus'])
									);
			}else{//是按重量计算邮费
				$item_ary['default'] = array(
										'express_start'=>intval($_POST['weight_express_start']),
										'express_postage'=>intval($_POST['weight_express_postage']),
										'express_plus'=>intval($_POST['weight_express_plus']),
										'express_postageplus'=>intval($_POST['weight_express_postageplus'])
									);	
			}
			$express_areas = $_POST['express_areas'];
			if(is_array($express_areas)){
				$express_start = $_POST['express_start'];
				$express_postage = $_POST['express_postage'];
				$express_plus = $_POST['express_plus'];
				$express_postageplus = $_POST['express_postageplus'];
				//$item_ary['list'] = '';
				require(INFO_PATH."cf_var_area.php");
				foreach($express_areas as $k=>$v){
					//获取相应的省份名字
					$area_ary = explode(',',$v);
					$areas_name = '';
					foreach($area_ary as $v2){
						$areas_name .= $cf_var_area[$v2]['title'].',';
					}
					$areas_name = trim($areas_name,',');
					
					$temp_ary = array(
									'express_areas_name'=>$areas_name,
									'express_areas'=>$v,
									'express_start'=>$express_start[$k],
									'express_postage'=>$express_postage[$k],
									'express_plus'=>$express_plus[$k],
									'express_postageplus'=>$express_postageplus[$k],
								);
					$item_ary['list'][$k] = $temp_ary;
				}				
			}
			
			
			$data=array(
						'fee_title'=>trim($_POST['fee_title']),
						'fee_is_free'=>'0',
						'fee_type'=>$fee_type,
						'fee_item'=>serialize($item_ary),
						'fee_edit_time'=>date("Y-m-d H:i:s"),
						'fee_editor'=>$cf_admin_info['admin_name'],
					);
	
			
		}
		
		$result = $main->update_data($data,"fee_id = '".$id."'");
		if ($result) {
			$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
			unset($_SESSION['pre_url']);			
			my_jump('修改成功', '', $return_page, 3);
	    } else {
			my_jump('修改失败');
	    }		
	  
	  
	    
	} else {
		$row = $main->get_data_row($id);
	   	$row['fee_item'] = unserialize($row['fee_item']);
		$smarty->mypage($cf_controller, $cf_action, 0);
	}
	break;
    case "del":
	$result = $main->del_data("fee_id='$id'");
	if ($result) {
	    my_jump('删除成功', '', $return_page, 1);
	} else {
	    my_jump('删除失败!');
	}

	break;
	case "detail":
	$smarty->mypage($cf_controller, $cf_action, 0);
	$row = $main->get_data_row($id);
	if (!$row)
	my_jump('沒有此信息');
	if($row['goods_photo']){
    $img_ary = explode("|",$row['goods_photo']);
		foreach($img_ary as $v){
			$img_list .= '<a href="'.$v.'" rel="lightbox-group" title="'.strip_tags($text_ary[$k]).'"><img src="'.get_thumb_name($v).'" height="30" /></a>&nbsp;&nbsp;'; 
		}
	}
	$row['img'] = $img_list;
	$row['cate'] = get_cate($row['goods_cate'],$row['goods_sub_cate']);
		if($row['goods_package'])
        $row['package'] = json_decode( stripslashes($row['goods_package']),true);
		if($row['goods_options'])
        $row['options'] = json_decode( stripslashes($row['goods_options']),true);
	//dump($row);
	break;  
    case "all":
		$alltype = $_POST['alltype'];
		$idlist = $_POST['idlist'];
		if($idlist){
			$list = implode(",",$idlist);
			switch($alltype){
				case "allspecial":
					$data = array('goods_isspecial'=>'1');
					$result = $main->update_data($data,"goods_id in (".$list.") and goods_isspecial='0' ");
				break;
				case "allunspecial":
					$data = array('goods_isspecial'=>'0');
					$result = $main->update_data($data,"goods_id in (".$list.") and goods_isspecial='1' ");
				break;
				case "allhot":
					$data = array('goods_ishot'=>'1');
					$result = $main->update_data($data,"goods_id in (".$list.") and goods_ishot='0' ");
				break;
				case "allunhot":
					$data = array('goods_ishot'=>'0');
					$result = $main->update_data($data,"goods_id in (".$list.") and goods_ishot='1' ");
				break;
				case "allup":
					$data = array('goods_status'=>'1');
					$result = $main->update_data($data,"goods_id in (".$list.") and goods_status='0' ");
				break;
				case "alldown":
					$data = array('goods_status'=>'0');
					$result = $main->update_data($data,"goods_id in (".$list.") and goods_status='1' ");
				break;
				case "alldel":
					$result = $main->del_data("goods_id in (".$list.") and goods_status='0'");
				break;
				default:
					my_jump('请指定操作方式');
				break;
			}	
			if($result)
				my_jump('操作完成','',$cf_self_page,1);
			else
				my_jump('操作失败');
			
		}else{
			my_jump('输入的参数有误');	
		}
	break;
	 case "province":
	 	$id = intval($get['id']);
		$row = $_GET['areas'];
		
	 	//dump($_GET);	
		$smarty->mypage('province', $cf_action, 0);
	break;
	default:
	my_jump('非法操作!');
	break;
}


$smarty->assign(array(
    'id' => $id,
    'row' => $row,
    'data_ary' => $data_ary,
	'cate_power' => $cate_power,
    'page' => $page,
    'self' => $self,
    'skey' => $skey,
    'skey2' => $skey2,
    'skey3' => $skey3,
    'skey4' => $skey4,
	'skey5' => $skey5,
	'hot' => $hot,
	'special' => $special,
	'cate' => $cate,
	'cate1' => $cate1,
	'cate2' => $cate2,
	'yesno' => add_array_item(array('' => '全部'), $cf_info_var_common['yesno']),
	'brand' => add_array_item(array('0' => '不指定'), change_array($cf_var_cate['3']['child'],'id','title')),
	'cate_array' => $cf_var_cate
));


$smarty->mydisplay();
?>